<extend name="Public/base" />
<block name="body">
<div class="main-title cf">
	<h2>
		<if condition="ACTION_NAME eq 'add'">新增 <else /> 编辑</if>
		模型
	</h2>
</div>

<!-- 标签页导航 -->
<div class="tab-wrap">
	<ul class="tab-nav nav">
		<li data-tab="tab1" class="current"><a href="javascript:void(0);">基 础</a></li>
		<li data-tab="tab2"><a href="javascript:void(0);">设 计</a></li>
        <li data-tab="tab3"><a href="javascript:void(0);">列表定义</a></li>
		<li><a href="{:U('Admin/attribute/index','model_id='.$info['id'])}">字段管理</a></li>
	</ul>
	<div class="tab-content">
		<!-- 表单 -->
		<form id="form" action="{:U('update')}" method="post"
			class="form-horizontal doc-modal-form">
			<!-- 基础 -->
			<div id="tab1" class="tab-pane in tab1">
				<div class="form-item cf">
					<label class="item-label">模型标识<span class="check-tips">（请输入文档模型标识）</span></label>
					<div class="controls">
						<input type="text" class="text input-large" name="name"
							value="{$info.name}">
					</div>
				</div>
				<div class="form-item cf">
					<label class="item-label">模型名称<span class="check-tips">（请输入模型的名称）</span></label>
					<div class="controls">
						<input type="text" class="text input-large" name="title"
							value="{$info.title}">
					</div>
				</div>
				<div class="form-item cf">
					<label class="item-label">所属插件</label>
					<div class="controls">
						<select name="addon">
							<option value="">无</option>
							<volist name="list" id="m">
							<option value="{$key}"<if
									condition="$info[addon] eq $key"> selected="selected"</if>
								>{$m}
							</option>
							</volist>
						</select>
					</div>
				</div>
			</div>
			<div id="tab2" class="tab-pane tab2">	
				<div class="form-item cf">
					<label class="item-label">新增按钮</label>
					<div class="controls">
                    <input type="radio" name="add_button" value="1" <if condition="$info[add_button]==1">checked="checked"</if> > 显示
                    <input type="radio" name="add_button" value="0" <if condition="$info[add_button]==0">checked="checked"</if> > 隐藏 
					</div>
				</div>
                <div class="form-item cf">
					<label class="item-label">批量删除按钮</label>
					<div class="controls">
                           <input type="radio" name="del_button" value="1" <if condition="$info[del_button]==1">checked="checked"</if> > 显示
                           <input type="radio" name="del_button" value="0" <if condition="$info[del_button]==0">checked="checked"</if> > 隐藏 
					</div>
				</div>  
                <div class="form-item cf">
					<label class="item-label">全选复选框</label>
					<div class="controls">
                           <input type="radio" name="check_all" value="1" <if condition="$info[check_all]==1">checked="checked"</if> > 显示
                           <input type="radio" name="check_all" value="0" <if condition="$info[check_all]==0">checked="checked"</if> > 隐藏 
					</div>
				</div>  
                <div class="form-item cf">
					<label class="item-label">搜索功能</label>
					<div class="controls">
                           <input type="radio" name="search_button" value="1" <if condition="$info[search_button]==1">checked="checked"</if> > 显示
                           <input type="radio" name="search_button" value="0" <if condition="$info[search_button]==0">checked="checked"</if> > 隐藏 
					</div>
				</div>            
				<div class="form-item cf">
					<label class="item-label">默认搜索字段<span class="check-tips">（默认列表模板的默认搜索项）</span></label>
					<div class="controls">
						<input type="text" class="text input-large" name="search_key" value="{$info.search_key}">
					</div>
				</div>
				<div class="form-item cf">
					<label class="item-label">列表每页条数<span class="check-tips">（默认列表模板的分页属性）</span></label>
					<div class="controls">
						<input type="text" class="text input-small" name="list_row" value="{$info.list_row|default=20}">
					</div>
				</div>
			</div>
            <div id="tab3" class="tab-pane tab3">				
				<div class="form-item cf">
					<label class="item-label">列表定义<span class="check-tips">（默认列表模板的展示规则）</span></label>
					<div style="margin: 15px 0; " class="specTable data-table">
						<table cellspacing="1" cellpadding="0">
							<thead>
								<tr>
                                    <th align="center" style="width: 150px">数据源</th>
									<th align="center">字段</th>
                                    <th align="center" style="width: 150px">列名</th>
									<th align="center" style="width: 100px">宽度</th>
									<th align="center" style="width: 150px">排序</th>
									<th align="center" style="width: 150px">操作</th>
								</tr>
							</thead>
							<tbody id="list_data_tbody">
								<volist name="list_grid" id="cd" key="sort">
                                <php>$name = $key;if($name=='ids'){ $cd['come_from']=1; }</php>
								<tr class="list_tr" rel="{$sort}">
									<td align="center">
                                    <select name="come_from[{$sort}]" class="select_type">
											<option value="0"<eq name="cd[come_from]" value="0">selected</eq> >表字段 </option>
											<option value="1"<eq name="cd[come_from]" value="1">selected</eq> >管理操作 </option>
									</select></td>
                                    <td align="center">
                                    <div class="select_field">
                                          <select name="field[{$sort}]" class="select_field_event">
                                              <volist name="fields" id="vo">
                                              <option value="{$key}"<eq name="name" value="$key">selected</eq> >{$vo.title}</option>
                                              </volist>
                                          </select>
                                    </div>
                                    <div class="custom_field">
                                    <table cellspacing="1" cellpadding="0">
                                        <tbody>
                                            <volist name="cd.href" id="vo">
                                            <tr class="list_tr_tr">
                                                <td align="center" style="width: 100px"><input type="text" value="{$vo.title}"  class="form-control" name="url_title[{$sort}][]" style="width:100px"></td>
                                                <td align="center" style="width: 150px"><input type="text" value="{$vo.url}"  class="form-control" name="url_url[{$sort}][]" style="width:150px"></td>
                                                         
                                                <td align="center" style="width: 30px"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up">↑</a>&nbsp;&nbsp;&nbsp;
                                                <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down">↓</a>&nbsp;&nbsp;&nbsp;</td>
                                                <td style="width: 30px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
                                            </tr>
                                            </volist>                              
                                            <tr>
                                                <td colspan="4"><a href="javascript:void(0);" onclick="add_tr_tr(this)">+增加链接</a></td>
                                            </tr>
                                        </tbody>
                                    </table> 
                                    </div>                                    
                                    </td>    
                                    <td align="center"><input type="text" value="{$cd.title}" class="form-control attr_title" name="attr_title[{$sort}]"></td>                                
									<td align="center"><input type="text" value="{$cd.width}" class="form-control" name="width[{$sort}]" placeholder="自动" style="width:50px"> px</td>
									<td><input type="radio" name="is_sort[{$sort}]" value="0" <if condition="$cd[is_sort]==0">checked="checked"</if> > 不可排序 
                                    <input type="radio" name="is_sort[{$sort}]" value="1" <if condition="$cd[is_sort]==1">checked="checked"</if> > 可排序</td>
									<td><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="move_up">↑</a>&nbsp;&nbsp;&nbsp;
                                    <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="move_down">↓</a>&nbsp;&nbsp;&nbsp;
                                    <a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
								</tr>
								</volist>
								<tr class="more_tr">
									<td colspan="6"><a href="javascript:add_tr()">+增加新列</a></td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>				
			</div>            

			<!-- 按钮 -->
			<div class="form-item cf">
				<label class="item-label"></label>
				<div class="controls edit_sort_btn">
					<input type="hidden" name="id" value="{$info['id']}" />
					<button class="btn submit-btn ajax-post no-refresh" type="submit"
						target-form="form-horizontal">确 定</button>
					<button class="btn btn-return"
						onclick="javascript:history.back(-1);return false;">返 回</button>
				</div>
			</div>
		</form>
	</div>
</div>
<table style="display: none">
	<tr id="default_tr">
        <td align="center">
        <select name="come_from[sort_id]" class="select_type">
                <option value="0" selected>表字段</option>
                <option value="1">管理操作</option>
        </select> 
        </td>
        <td align="center">
        <div class="select_field">
        <select name="field[sort_id]" class="select_field_event">
                <volist name="fields" id="vo">
                <option value="{$key}">{$vo.title}</option>
                </volist>
        </select>
        </div>
        <div class="custom_field">
        <table cellspacing="1" cellpadding="0">
            <tbody>
                <tr class="list_tr_tr">
                    <td align="center" style="width: 100px"><input type="text" value="编辑"  class="form-control" name="url_title[sort_id][]" style="width:100px"></td>
                    <td align="center" style="width: 150px"><input type="text" value="[EDIT]"  class="form-control" name="url_url[sort_id][]" style="width:150px"></td>
                             
                    <td align="center" style="width: 30px"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up">↑</a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down">↓</a>&nbsp;&nbsp;&nbsp;</td>
                    <td style="width: 30px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
                </tr>
                <tr class="list_tr_tr">
                    <td align="center" style="width: 100px"><input type="text" value="删除"  class="form-control" name="url_title[sort_id][]" style="width:100px"></td>
                    <td align="center" style="width: 150px"><input type="text" value="[DELETE]"  class="form-control" name="url_url[sort_id][]" style="width:150px"></td>
                             
                    <td align="center" style="width: 30px"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up">↑</a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down">↓</a>&nbsp;&nbsp;&nbsp;</td>
                    <td style="width: 30px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
                </tr>
                <tr class="list_tr_tr">
                    <td align="center" style="width: 100px"><input type="text" value="预览"  class="form-control" name="url_title[sort_id][]" style="width:100px"></td>
                    <td align="center" style="width: 150px"><input type="text" value="preview?id=['id']"  class="form-control" name="url_url[sort_id][]" style="width:150px"></td>
                             
                    <td align="center" style="width: 30px"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up">↑</a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down">↓</a>&nbsp;&nbsp;&nbsp;</td>
                    <td style="width: 30px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
                </tr>                                
                <tr>
                    <td colspan="4"><a href="javascript:void(0);" onclick="add_tr_tr(this)">+增加链接</a></td>
                </tr>
            </tbody>
        </table> 
        </div>
        </td>
        <td align="center"><input type="text" value=""  class="form-control attr_title" name="attr_title[sort_id]"></td>
        <td align="center"><input type="text" value="" class="form-control" name="width[sort_id]" placeholder="自动" style="width:50px"> px</td>
        <td>
        <input type="radio" name="is_sort[sort_id]" value="0" checked="checked"> 不可排序 
        <input type="radio" name="is_sort[sort_id]" value="1"> 可排序</td>
        <td><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="move_up">↑</a>&nbsp;&nbsp;&nbsp;
        <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="move_down">↓</a>&nbsp;&nbsp;&nbsp;
        <a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
	</tr>
    <tr id="default_tr_tr">
    <td align="center" style="width: 100px"><input type="text" value=""  class="form-control" name="url_title[sort_id][]" style="width:100px"></td>
    <td align="center" style="width: 150px"><input type="text" value=""  class="form-control" name="url_url[sort_id][]" style="width:150px"></td>             
    <td align="center" style="width: 30px"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up">↑</a>&nbsp;&nbsp;&nbsp;
    <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down">↓</a>&nbsp;&nbsp;&nbsp;</td>
    <td style="width: 30px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
</tr>
</table>

</block>
<block name="script"> 
<script type="text/javascript" src="__STATIC__/jquery.dragsort-0.5.1.min.js"></script> 
<script	type="text/javascript" charset="utf-8">


function init(){
	hide_move()
	hide_sub_move()
	
	$('.select_type').each(function(){ select_type(this); });
	$('.select_type').change(function(){ select_type(this); });
	
	$('.select_field_event').each(function(){ select_field_event(this); });
	$('.select_field_event').change(function(){ select_field_event(this); });
}

$(function(){
    showTab()
	//add_tr()
	init()
	
	//拖曳插件初始化
	$(".needdragsort").dragsort({
	     dragSelector:'li',
	     placeHolderTemplate: '<li class="draging-place">&nbsp;</li>',
	     dragBetween:true,	//允许拖动到任意地方
	     dragEnd:function(){
	    	 var self = $(this);
	    	 self.find('input').attr('name', 'field_sort[]');
	     }
	 });	
});

//增加问题
var tr_sort_id = 0;
function add_tr(){
	var list_count = 0;
	$('.list_tr').each(function() {
		list_count += 1;
		var sort_id = $(this).attr('rel');
		if(sort_id>tr_sort_id) tr_sort_id = sort_id;
    });	
	
	tr_sort_id += 1;
	
	re = new RegExp("sort_id", "g");
	str  = $('#default_tr').html().replace(re, tr_sort_id);
	//console.log(str);
	var html = '<tr class="list_tr" rel="'+tr_sort_id+'">'+ str +'</tr>';
	if(list_count==0)
	  $('#list_data_tbody tr').before(html);	
	else
	  $('.list_tr:last').after(html);
	  
	init()
}
function add_tr_tr(_this){
    var tr_sort_id = $(_this).parents('.list_tr').attr('rel')
	
	re = new RegExp("sort_id", "g");
	str  = $('#default_tr_tr').html().replace(re, tr_sort_id);
	console.log(str);
	var html = '<tr class="list_tr_tr" rel="'+tr_sort_id+'">'+ str +'</tr>';

	$(_this).parent().parent().parent().before(html);	

	  
	init()
}
//删除问题
function remove_tr(_this){	
	$(_this).parent().parent().remove();
	init()
}
//排序--向上
function move_up(obj) { 
  var objParentTR = $(obj).parent().parent(); 
  var prevTR = objParentTR.prev(); 
  if (prevTR.length > 0) { 
	prevTR.insertAfter(objParentTR); 
  }
  init()
} 
//排序--向下
function move_down(obj) { 
  var objParentTR = $(obj).parent().parent(); 
  var nextTR = objParentTR.next(); 
  if (nextTR.length > 0) { 
	nextTR.insertBefore(objParentTR); 
  } 
  init()
} 
//第一行只显示向下，最后一行只显示向上
function hide_move(){
	$('.move_up').each(function() {
		$(this).show();
    });
	$('.move_down').each(function() {
		$(this).show();
    });	
	$('.list_tr:first').find('.move_up').hide();
	$('.list_tr:last').find('.move_down').hide();
}
function hide_sub_move(){
	$('.sub_move_up').each(function() {
		$(this).show();
    });
	$('.sub_move_down').each(function() {
		$(this).show();
    });	
	$('.custom_field').each(function() {
          $(this).find('.list_tr_tr:first').find('.sub_move_up').hide();
	      $(this).find('.list_tr_tr:last').find('.sub_move_down').hide();
    });
}

//选择问题类型
function select_type(_this){
	var type = $(_this).val();
	
	var input = $(_this).parents('.list_tr').find('.attr_title')
	var old_val = input.val()
	if(type==1){
		$(_this).parent().next().find('.select_field').hide();
		$(_this).parent().next().find('.custom_field').show();
		
		if(old_val==''){
			input.val('操作')
		}
	}else{
		$(_this).parent().next().find('.select_field').show();
		$(_this).parent().next().find('.custom_field').hide();
		
		if(old_val=='操作'){
			input.val('')
		}
	}
}
function select_field_event(_this){
	var name = $(_this).find("option:selected").text()
	var input = $(_this).parents('.list_tr').find('.attr_title')
	var old_val = input.val()

	if(old_val==''){
	    input.val(name)
	}else{
		var flat = false
		$(_this).find('option').each(function (){  

			if($(this).text()==old_val){
			    flat = true 
			}
        });
		
		if(flat){
			input.val(name)
		}
	}
}
</script></block>
